﻿using lBLL;
using lDAL;
using Model;
using System;

namespace BLL
{
    public class UserBLL: lUserBLL
    {
        public UserBLL(lUserDAL<Userinfor> db , lUserDAL<Hoteinfor> dbs , lUserDAL<Shanginfor> dbss)
        {
            Db = db;
            Dbs = dbs;
            Dbss = dbss;
        }

        public lUserDAL<Userinfor> Db { get; }
        public lUserDAL<Hoteinfor> Dbs { get; }
        public lUserDAL<Shanginfor> Dbss { get; }

        public Task<int> AddAsync(Userinfor userinfor)
        {
            return Db.AddAsync(userinfor);
        }

        public Task<int> AddAsyncss(Shanginfor shanginfor)
        {
            return Dbss.AddAsync(shanginfor);
        }

        public List<Shanginfor> Dao()
        {
            return Dbss.GetAllAsync().ToList();
        }

        public Task<int> DeleteAsync(int id)
        {
            var a = Db.GetAllAsync().FirstOrDefault();
            return Db.DeleteAsync(a);
        }

        public IQueryable GetAllAsync(string? name, int? xia)
        {
            var user = Db.GetAllAsync();
            var hote = Dbs.GetAllAsync();
            var list = from a in user
                       join b in hote
                       on a.HoteId equals b.HoteId
                       select new
                       {
                           a.UserId,
                           b.HoteId,
                           a.UserName,
                           a.UserBao,
                           a.UserFen,
                           a.UserSho,
                           a.UserStart,
                           a.UserGeng,
                           a.UserTime,
                           a.UserImg,
                       };
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(x=>x.UserName.Contains(name));
            }
            if (xia > 0)
            {
                list = list.Where(x=>x.HoteId == xia);
            }
            return list;
        }

        public dynamic GetAllAsyncList(string? names, int pageindex, int pagesize)
        {
            var list = Dbss.GetAllAsync().Where(x=>x.ShanDele==0);
            if (!string.IsNullOrEmpty(names))
            {
                list = list.Where(x=>x.ShanName.Contains(names));
            }
            var count = list.Count();
            var pagecount = list.OrderBy(x=>x.ShanId).Skip((pageindex-1)* pagesize).Take(pagesize);
            return new
            {
                count,
                pagecount
            };
        }

        public Task<int> LjDele(int id)
        {
            var a = Dbss.GetAllAsync().FirstOrDefault(x=>x.ShanId==id);
            a.ShanDele = 1;
            return Dbss.UpdateAsnc(a);
        }

        public Task<int> Plzt(string idstring)
        {
            List<int> ids = idstring.Split(',').Select(x => Convert.ToInt32(x)).ToList();
            var list = Dbss.GetAllAsync().Where(x => ids.Contains(x.ShanId)).ToList();
            foreach (var item in list)
            {
                if (item.ShanStart==1)
                {
                    item.ShanStart = 2;
                }
                else
                {
                    item.ShanStart = 1;
                }
            }
            return Dbss.UpdateAsncAsync(list);
        }

        public Task<int> UpdateAsnc(Userinfor userinfor)
        {
            return Db.UpdateAsnc(userinfor);
        }
    }
}
